var idea = (function() {
    return {
        addTask: function(limit) {
            $.post('/ajax/createtask', {"task_limit" : limit}, function (data) {
                if (data.result == 'ok') {
                    idea.getForm(data.id, 'task', 'add');
                }
            });
        },
        getForm: function (id, type, mode) {
            var title = '';
            switch (mode) {
                case 'add':
                    title = 'Добавить задачу';
                    break;
                case 'edit':
                    title = 'Редактировать задачу';
                    break;
            }

            $.get('/ajax/getform',{"id": id, "type": type}, function (data) {
                modal.open(data.html, {"title": title});
                return null;
            });
        },
        saveTask : function (params) {
            params = params || {
                task_id          : $('#task-id').val(),
                task_title       : $('#task-title').val(),
                task_description : $('#task-content').val(),
                task_notify      : $('#task-notify').val(),
                task_type        : $('#task-type').val(),
                task_deadline    : $('#task-deadline').val(),
                task_priority    : $('#task-priority').val(),
                task_limit       : $('#task-limit').val()
            };

            $.get('/ajax/savetask',{params: params}, function (data) {
                if (data.result == 'ok') {

                    if (modal.isOpen()) {
                        modal.close();
                    }

                    mn.notify('success', {"text" :"Изменения сохранены"});
                }
            });
        },
        editTask: function (id) {
            idea.getForm(id, 'task', 'edit');
        },
        deleteTask: function (id) {
            if (!id) {
                return;
            }

            var blockTerm = $('#task-id-'+ id +'');
            $.post('/ajax/deletetask', {"task_id" : id}, function (data) {
                if (data.result == 'ok') {
                    if (blockTerm.length) {
                        blockTerm.hide('slow');
                    }

                    mn.notify('success', {text: "Задача удалена"});
                }
            });
        },
        deleteTaskForm: function (id) {
            if (!id) {
                return;
            }

            idea.deleteTask(id);
            modal.close();

        },
        taskToArchive: function (id) {
            if (!id) {
                return;
            }

            var blockTerm = $('#task-id-'+ id +'');
            $.post('/ajax/tasktoarchive', {"task_id" : id}, function (data) {
                if (data.result == 'ok') {
                    if (blockTerm.length) {
                        blockTerm.hide('slow');
                    }

                    mn.notify('success', {text: "Задача помещена в архив"});
                }
            });
        }
   }
}());